package com.dengdai.applibrary.activitys;

import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.adapter.BaseCommAdapter;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.GridViewInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.NetWorkUtils;
import com.dengdai.applibrary.widgets.custom.LoadDataEmptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshGridView;

/**
 * @说明 GridView基础类
 * @作者 LY
 * @时间 16/5/21 16:34
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseGridViewActivity<T> extends BaseActivity implements GridViewInterface {
    protected PullToRefreshGridView pullToRefreshGridView;
    protected LoadDataEmptyView loadDataView;
    protected BaseCommAdapter baseCommAdapter;
    protected int page = 0;

    @Override
    public int getLayoutResID() {
        return BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.BASE_GRIDVIEW_ACTIVITY);
    }

    @Override
    public void initData() {
        pullToRefreshGridView = (PullToRefreshGridView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.PULLTOREFRESHGRIDVIEW));
        loadDataView = (LoadDataEmptyView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.LOADDATAEMPTY));
    }

    @Override
    public void business() {
        //设置每行的数目
        pullToRefreshGridView.getRefreshableView().setNumColumns(getNumColumns());
        //设置Adapter
        baseCommAdapter = getAdapter();
        pullToRefreshGridView.getRefreshableView().setAdapter(baseCommAdapter);
        //设置默认页码
        page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
        //设置刷新类型
        if (getGridViewMode() != null) {
            pullToRefreshGridView.setMode(getGridViewMode());
        } else {
            pullToRefreshGridView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        //设置刷新
        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {//下拉刷新
                page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
                baseCommAdapter.clear();
                loadingData();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {//上拉加载
                loadingData();
            }
        });
        //设置点击事件
        pullToRefreshGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemClick(baseCommAdapter.getListData().get(position), position);
            }
        });
        {//加载数据
            loadDataView.setVisibility(View.VISIBLE);
            if (NetWorkUtils.getNetworkStatus(BaseApplication.getAppContext())) {
                loadDataView.loadingData();
                loadingData();
            } else {
                loadDataView.netWorkError();
            }
        }
    }

    @Override
    public void stopRefreshView() {
        pullToRefreshGridView.onRefreshComplete();
        if (baseCommAdapter.getCount() > 0) {
            loadDataView.setVisibility(View.GONE);
        } else {
            loadDataView.setVisibility(View.VISIBLE);
            loadDataView.loadDataError();
        }
    }
}
